package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.ArchivesRecord;
import com.tuocent.dagv2.entity.FieldConfig;
import com.tuocent.dagv2.mapper.ArchivesRecordMapper;
import com.tuocent.dagv2.service.IArchivesRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuocent.dagv2.service.IFieldConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Consumer;

import static com.tuocent.dagv2.common.utils.PublicUtil.sortMap;

/**
 * <p>
 * 档案文件目录 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-09-12
 */
@Service
public class ArchivesRecordServiceImpl extends ServiceImpl<ArchivesRecordMapper, ArchivesRecord> implements IArchivesRecordService {
    @Autowired
    private IFieldConfigService fieldConfigService;

    @Override
    public AjaxResult getArchivesRecord(Map<String, Object> params) {
        int page = PublicConst.DEF_PAGE;
        int limit = PublicConst.DEF_LIMIT;

        String classifyId = params.getOrDefault("classifyId", "").toString();
        String value = "";
        QueryWrapper<ArchivesRecord> query = new QueryWrapper<ArchivesRecord>()
                .eq("is_del", "0");
        for (Object obj : params.keySet()) {
            String key = obj.toString();
            String keyValue = params.get(key).toString();
            if (keyValue.trim().isEmpty()) {
                continue;
            }
            switch (key) {
                case "page":
                    page = Integer.parseInt(keyValue);
                    break;
                case "limit":
                    limit = Integer.parseInt(keyValue);
                    break;
                case "value":
                    value = keyValue;
                    break;
                case "fileArchivalCode":
                    query.like("file_archival_code", keyValue);
                    break;
                case "responsibleperson":
                    query.like(key, keyValue);
                    break;
                default:
                    query.eq(key, keyValue);
                    break;
            }
        }

        //获取字段配置
        List<FieldConfig> fieldsList = fieldConfigService.getFieldConfig(classifyId, "1");
        Map<String, String> fastsearch = new HashMap<>();
        Map<String, Integer> sort = new HashMap<>();
        Collection<String> mixSort = new ArrayList<>();
        //获取排序、检索字段
        for (FieldConfig filed : fieldsList) {
            //排序
            if ("1".equals(filed.getIsSort())) {
                int dix = 999;
                if (filed.getSortIdx() != null) {
                    dix = filed.getSortIdx();
                }
                sort.put(filed.getFieldName(), dix);
            }
            if ("1".equals(filed.getIsmixSort())) {
                mixSort.add(filed.getFieldName());
            }
            if ("1".equals(filed.getIsFastsearch())) {
                fastsearch.put(filed.getFieldName(), filed.getIsVaguesearch());
            }
        }

        //检索,如果没有可快速查询字段，侧返回空值
        if (!value.isEmpty()) {
            if (!fastsearch.isEmpty()) {
                String finalValue = value;
                Consumer<QueryWrapper<ArchivesRecord>> wrapper = new Consumer<QueryWrapper<ArchivesRecord>>() {
                    @Override
                    public void accept(QueryWrapper<ArchivesRecord> archivesFilesQueryWrapper) {
                        for (String key : fastsearch.keySet()) {
                            if ("1".equals(fastsearch.get(key))) {
                                archivesFilesQueryWrapper.or().like(key, finalValue);
                            } else {
                                archivesFilesQueryWrapper.or().eq(key, finalValue);
                            }
                        }
                    }
                };
                query.and(wrapper);
            }
        }
        //排序
        if (!sort.isEmpty()) {
            Map<String, Integer> sortField = sortMap(sort);
            for (String key : sortField.keySet()) {
                if (mixSort.contains(key)) {
                    query.orderByAsc("length(" + key + ")," + key);
                } else {
                    query.orderByAsc(key);
                }
            }
        }
        Page<ArchivesRecord> archiveRecord = page(new Page<>(page, limit), query);
        return AjaxResult.success(archiveRecord.getRecords(), archiveRecord.getTotal());
    }
}
